var t = require('../@babel/runtime/helpers/typeof.js'),
  e = (getApp(), require('501CABE770B67DCF367AC3E016121B27.js'));
module.exports = Behavior({
  properties: {
    skuItems: Array,
    sku: Array,
    showSku: Boolean,
    goodsId: String,
    cart_id: String,
    stockType: { type: String, value: '2' },
    pinParam: { type: String, value: '' },
    thumb: String,
    skuType: { type: String, value: 'old' },
    btnType: { type: String, value: 'buy' },
    cameFrom: String,
    goodsNumLimit: Object,
    insuranceInfo: { type: Object, value: {} },
    buy_insurance_default: { type: Boolean, value: !1 },
    isGiveInsurance: Number,
    shopPrice: Number,
    stock: Number,
  },
  options: { pureDataPattern: /^__/ },
  observers: {
    skuRes: function (t) {
      this.data.__oldSkuPrice =
        t && t.hasOwnProperty('shop_price') ? t.shop_price : 0;
    },
    'showSku, isGiveInsurance, insuranceInfo, stockType': function (
      t,
      e,
      s,
      i,
    ) {
      var n = this;
      t &&
        setTimeout(function (t) {
          n.setData({ sku_anchor: 'scroll_anchor_top' });
        }, 600),
        i != this.data._stockType &&
          ((this.data._stockType = i),
          this.setData({ stock_type: '1' === i ? 'number' : 'chinese' }));
    },
  },
  data: {
    need_total_weidou: 0,
    _stockType: 0,
    __oldSkuPrice: 0,
    __couponList: [],
    show: !0,
    _insuranceInfo: {},
    buyBtnType: 0,
    coupon: {},
    submitHint: '请选择规格',
    sku_loading: !0,
    showImg: '',
    sku_id: null,
    num: 1,
    skuRes: {},
    stockMaxNum: 0,
    buy_insurance: !1,
    protocol: [],
    sku_anchor: 'scroll_anchor_bottom',
    stock_type: 'number',
    stock_number: +wx.getStorageSync('initSystem').stock_number || 1500,
    skuMap: {},
    skuCombin: [],
    choosedCombin: [],
    skuTitle: '',
  },
  pageLifetimes: {},
  created: function () {},
  attached: function () {
    this.init();
  },
  ready: function () {},
  detached: function () {
    console.log('sku detached in'),
      this.setData({
        sku_id: null,
        num: 1,
        skuRes: {},
        stockMaxNum: 0,
        buy_insurance: !1,
      });
  },
  methods: {
    setSkuCombin: function () {
      var t = [],
        e = this.data.sku.reduce(function (e, s) {
          var i = s.attrData
            .map(function (t) {
              return t.attr_id;
            })
            .sort(function (t, e) {
              return +t - Number(e);
            })
            .join(',');
          return (e[i] = s), +s.stock > 0 && t.push(i), e;
        }, {});
      (this.data.skuMap = e), (this.data.skuCombin = t);
    },
    skuInit: function (t) {
      var e = this.data,
        s = e.skuItems,
        i = e.sku;
      e.btnType;
      this.setSkuCombin();
      var n = null,
        o = i.filter(function (t) {
          return t.stock > 0;
        });
      if ((o.length && (n = o[0]), t)) {
        var a = i.find(function (e) {
          return +e.id == +t;
        });
        a && a.stock > 0
          ? ((n = a), this.setChoosedCombin(n))
          : (this.setChoosedCombin(), console.log('复购回填失败', a, a.stock));
      }
      if (0 === s.length)
        return (
          this.setData({ sku_id: n.id, skuRes: n, stockMaxNum: n.stock }),
          void this.triggerEvent('getSkuRes', {
            pName: n ? n.properties_name : '',
            skuRes: n,
          })
        );
      1 === i.length && o.length
        ? this.setChoosedCombin(n)
        : this.setChoosedCombin();
    },
    setChoosedCombin: function (t) {
      var e = this;
      t &&
        (this.data.choosedCombin = t.attrData.map(function (t) {
          return t.attr_id;
        })),
        this.data.skuItems.forEach(function (t) {
          t.values.forEach(function (t) {
            e.data.choosedCombin.length &&
            e.data.choosedCombin.includes(t.attribute_id)
              ? (t.active = 1)
              : (t.active = 0);
          });
        }),
        this.setSkuItemsStatus();
    },
    onActive: function (t) {
      var e = t.target.dataset,
        s = e.index,
        i = e.i,
        n = this.data.skuItems,
        o = n[s].values[i];
      2 !== o.active &&
        (1 === o.active
          ? (o.active = 0)
          : (n[s].values.forEach(function (t) {
              t.active = 0;
            }),
            (o.active = 1)),
        (this.data.choosedCombin = n.reduce(function (t, e) {
          var s = e.values.find(function (t) {
            return 1 === t.active;
          });
          return s && t.push(s.attribute_id), t;
        }, [])),
        this.setSkuItemsStatus());
    },
    setSkuItemsStatus: function () {
      var t = this;
      console.time('setSkuItemsStatus 所用时间');
      var s,
        i = this.data,
        n = i.skuItems,
        o = i.choosedCombin,
        a = i.skuMap,
        u = [],
        r = [];
      (s =
        a[
          o
            .sort(function (t, e) {
              return +t - +e;
            })
            .join(',')
        ] || null),
        n.forEach(function (e) {
          var s = e.values.find(function (t) {
            return 1 === t.active;
          });
          s
            ? (r.push(s.attribute_value),
              e.values.forEach(function (e) {
                if (1 !== e.active) {
                  var i = o.join(',').replace(s.attribute_id, e.attribute_id);
                  t.isCombinExist(i.split(','))
                    ? (e.active = 0)
                    : (e.active = 2);
                }
              }))
            : (u.push(e.attr_name),
              e.values.forEach(function (e) {
                var s = o.concat([e.attribute_id]);
                t.isCombinExist(s) ? (e.active = 0) : (e.active = 2);
              }));
        }),
        console.timeEnd('setSkuItemsStatus 所用时间'),
        console.log('skuItems: ', n),
        this.setData(
          {
            skuItems: n,
            sku_loading: !1,
            skuTitle: s
              ? '已选 '.concat(r.join(' '))
              : '请选择 '.concat(u.join(' ')),
            skuRes: s,
            sku_id: s ? s.id : null,
            stockMaxNum: s ? s.stock : this.data.stock,
            nowStock: s ? s.stock : this.data.stock,
          },
          function () {
            t.resetGoodsNum(),
              t.triggerEvent('getSkuRes', {
                pName: n ? n.properties_name : '请选择 '.concat(u.join(' ')),
                skuRes: n,
              });
            var s = t.data,
              i = s.goodsType,
              n = s.skuRes,
              o = s.shopPrice,
              a = s.num;
            if ('weidou' === i.type) {
              var r = n ? n.shop_price : o;
              t.setData({ need_total_weidou: +e(r).times(a) });
            }
          },
        );
    },
    isCombinExist: function (e) {
      for (
        var s = this.data.skuCombin,
          i = function (t) {
            if (
              e.every(function (e) {
                return s[t].includes(e);
              })
            )
              return { v: !0 };
          },
          n = 0;
        n < s.length;
        n++
      ) {
        var o = i(n);
        if ('object' === t(o)) return o.v;
      }
      return !1;
    },
    resetGoodsNum: function () {
      var t = this.selectComponent('#GoodsNum');
      t &&
        +t.data.inputNum > +this.data.stockMaxNum &&
        t._numChange(+this.data.stockMaxNum, !0);
    },
    numChange: function (t) {
      var s = this,
        i = this.data.num,
        n = this.data,
        o = n.goodsType,
        a = n.skuRes,
        u = n.shopPrice,
        r = t.detail.thisNum;
      if (
        (this.setData({ num: r }),
        i != r && 'new' === this.data.skuType && this.setBtnStatus(),
        'weidou' === o.type)
      ) {
        var c = a ? a.shop_price : u;
        this.setData({ need_total_weidou: +e(c).times(r) }, function () {
          s.payInit();
        });
      }
    },
  },
});
